import time
from selenium import webdriver

class DouYu():

    def __init__(self):
        self.url = 'https://www.douyu.com/directory/all'
        self.driver = webdriver.Chrome()

    def get_data(self):
        li_list = self.driver.find_elements_by_xpath('//li[@class="layout-Cover-item"]')
        data_list = []
        for li in li_list:
            item = {}
            item['title'] = li.find_element_by_xpath('.//h3[@title]').get_attribute('title')
            item['topic'] = li.find_element_by_xpath('.//span[@class="DyListCover-zone"]').text
            item['author'] = li.find_element_by_xpath('.//div[@class="DyListCover-userName"]').text
            item['hot'] = li.find_element_by_xpath('.//span[@class="DyListCover-hot"]').text
            data_list.append(item)
        next_page = self.driver.find_element_by_xpath('//span[contains(text(),"下一页")]')
        return data_list, next_page

    def save_data(self, data_list):
        for data in data_list:
            print(data)

    def run(self):
        # 1.发送请求
        self.driver.get(self.url)
        time.sleep(30)
        # 2.获取数据
        # 3.提取数据
        data_list, next_page = self.get_data()
        # 4.保存数据
        while next_page is not None:
            self.save_data(data_list)
            next_page.click()
            time.sleep(30)
            data_list, next_page = self.get_data()
            # self.save_data(data_list)

if __name__ == "__main__":
    douyu = DouYu()
    douyu.run()